package com.songlanyun.modules.account.controller.api;

import cn.hutool.core.util.ObjectUtil;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.validator.group.AppGroup;
import com.songlanyun.modules.account.entity.AccountEntity;
import com.songlanyun.modules.account.entity.UserVerifiedEntity;
import com.songlanyun.modules.account.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.common.validator.group.AddGroup;
import com.songlanyun.common.validator.group.UpdateGroup;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.account.model.dto.UserVerifiedDTO;
import com.songlanyun.modules.account.service.UserVerifiedService;

import java.util.HashMap;
import java.util.Map;

/**
 * 实名认证
 *
 * @author lijiahe
 * @date 2022-09-22
 */
@RestController
@RequestMapping("api/account/verified")
public class UserVerifiedApiController extends AbstractApiController {
    @Autowired
    private UserVerifiedService userVerifiedService;

    @Autowired
    private AccountService accountService;

    /**
     * 实名认证
     */
    @PostMapping("/realNameAuth")
    public R realNameAuth(@RequestBody UserVerifiedDTO dto) {
        ValidatorUtils.validateEntity(dto, AppGroup.class);
        dto.setId(getUserId(true));
        String mobile = getMember().getMobile();
        dto.setMobile(mobile);
        userVerifiedService.realNameAuth(dto);
        return R.ok();
    }

    /**
     * 通过手机号获得真实性名
     */
    @PostMapping("/mobilemapname/{mobile}")
    public R mobileMapName(@PathVariable("mobile") String mobile) {
        AccountEntity account = accountService.getByMobile(mobile);
        if(ObjectUtil.isNull(account))
        {
            throw new RRException("手机号找不到对应的用户");
        }
        UserVerifiedEntity userVerifiedEntity = userVerifiedService.loadById(account.getId(), false);
        if(ObjectUtil.isNull(userVerifiedEntity))
        {
            throw new RRException("该手机号的用户没有实名认证");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("user_obj_id",userVerifiedEntity.getId());
        map.put("accept_real_name",userVerifiedEntity.getRealName());
        return R.ok().put("data",map);
    }

    /**
     * 详情
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        return R.ok().put("data", userVerifiedService.info(id));
    }

}
